/**
  ******************************************************************************
  * @file    stm32f0xx_uart.h
  * @author  MCD Application Team
  * @version V1.6.0
  * @date    15-April-2016
  * @brief  This file contains all the functions prototypes for the ADC firmware 
  *          library.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F0xx_UART_H
#define __STM32F0xx_UART_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx.h"

#define C_DEV_SPEED_ADDR      0x01
#define C_DEV_LEVEL_ADDR      0x01
#define HAL_UART_BUF_SIZE     128
   
#define C_PI                          3.141592654
#define C_CALCULATE_PERIOD            1
//BLE
#define BLE_CMD_MASK                  0x0F
#define BLE_SET_SYSTEM                0x00
#define BLE_SET_CANEL                 0x01
#define BLE_SET_RANGE                 0x02
#define BLE_SET_TOLERANCE             0x03
#define BLE_CLEAR_FLOW                0x04
#define BLE_READ_STATUS               0x05
#define BLE_SET_MINSPEED              0x06
#define BLE_SET_SPEED                 0x07
#define BLE_SET_SPEEDPARAM            0x08//02 len cmd addr(2) data(2)
#define BLE_READ_SPEEDPARAM           0x09
#define BLE_READ_POSNEG               0x0A
   
#define BLE_SYSTEM_VERSION            0x0F
   
#define BLE_READ_BIT                  0x10
#define BLE_RESPONSE_BIT              0x80
   
#define BLE_MIN_SIZE                  4
   
#define BLE_IDX_SOP                   0x02

//ModBus
#define MODBUS_READ_HOLDING						0x03
#define MODBUS_READ_INPUT							0x04
#define MODBUS_WRITE_SINGLE_HOLDING		0x06
#define MODBUS_WRITE_MORE_HOLDING			0x16

#define MODBUS_MIN_SIZE             4
#define MODBUS_WRITE_SIZE           8
   
#define ACCESS_CONTROL_READ         0x01  // attribute can be read
#define ACCESS_CONTROL_WRITE        0x02  // attribute can be written
//Bus
#define REG_BUS_LEVEL               0x0000
#define REG_BUS_SPEED               0x0001
#define REG_BUS_FLOW_HI             0x0002
#define REG_BUS_FLOW_LO             0x0003
#define REG_BUS_SUMFLOW_HI          0x0004
#define REG_BUS_SUMFLOW_LO          0x0005
#define REG_BUS_DEVADDR             0x0006
#define REG_BUS_BAUDRATE            0x0007
#define REG_BUS_CNL_MODE            0x0008
#define REG_BUS_CNL_R               0x0009
#define REG_BUS_CNL_RECTWIDE        0x000A
#define REG_BUS_CNL_TRAPEZIUM_1WIDE 0x000B
#define REG_BUS_CNL_TRAPEZIUM_2WIDE 0x000C
#define REG_BUS_CNL_TRAPEZIUM_DEPTH 0x000D
#define REG_BUS_EMPTY_BACKUP        0x000E
#define REG_BUS_RANGE_BACKUP        0x000F
   
#define REG_BUS_FIRST_LEVEL         0x0029
#define REG_BUS_EMPTY_LEVEL         0x002A
#define REG_BUS_RANGE               0x002B
#define REG_BUS_LOW_RANGE           0x002C
#define REG_BUS_RANGE_TOLERANCE     0x002D
#define REG_BUS_METER_STATUS        0x002E
#define REG_BUS_CLEAR_FLOW          0x002F
//Speed
#define REG_ROLL_ANGLE              0x0009
#define REG_PITCH_ANGLE             0x000A
#define REG_SURFACE_SPEED           0x000F
#define REG_FLOW_DIERECTION         0x000C
#define REG_SET_FLOW_DIERECTION     0x005E
#define REG_SET_PITCH_ANGLE         0x0055
#define REG_LOCK_ANGLE              0x005C
#define REG_SET_DEVADDR             0x0041
#define REG_SET_BAUDRATE            0x0004
#define REG_SET_SENSITIVTY          0x0051
#define REG_SET_MAX_SPEED           0x005F
#define REG_FILER_SPEED             0x01F4
#define REG_SOURCE_SPEED            0x01F6
#define REG_STRENGTH_SPEED          0x01F9
   
#define REG_SPEED_SENSITIVITY       0x0250
#define REG_SPEED_ANGLE             0x0252
#define REG_SPEED_GAIN              0x025C
#define REG_SPEED_POWER             0x025E
   
#define REG_SPEED_VERSION           0xEA60
   
#define REG_SPEED_WRITE             0x0000
//Water Level
#define REG_LEVEL_FILTER            0x03E8

//Speed
#define C_SPEED_MAX                 20000UL//mm/s
#define C_SPEED_MIN                 100UL//100UL//mm/s
#define C_SPEED_TORENCE             5000UL
   
enum{
  C_SPEED_SPEED,
  C_SPEED_DIERECTION,
  C_SPEED_SENSITIVITY,
  C_SPEED_ANGLE,
  C_SPEED_GAIN,
  C_SPEED_POWER,
};
//Data Type
#define DATATYPE_UINT8              1
#define DATATYPE_UINT16             2
#define DATATYPE_UINT32             4
enum{
  C_BAUDRATE_4800,
  C_BAUDRATE_9600,
  C_BAUDRATE_115200,
};
//Canel
enum{
  C_CANEL_ROUND,
  C_CANEL_RECTANGLE,
  C_CANEL_TRAPEZOID,
  C_CANEL_U,
  C_CANEL_AREA,
};
typedef struct
{
  uint16_t  reg;    // Real cluster ID
  uint8_t   accessControl;  // Read/write - bit field
  uint8_t   dataType;       // Data Type - defined in AF.h
  void      *dataPtr;       // Pointer to data field
}modBusAttrRec_t;

typedef struct{
  uint8_t mode;
  uint16_t radius;//cm
  uint16_t recWide;//cm
  uint16_t trapeDown;//cm
  uint16_t trapeUp;//cm
  uint16_t depth;//cm
  uint16_t area;//m2
}canel_t;

typedef struct{
  uint8_t addr;
  uint8_t baudrate;
}devInfo_t;

typedef struct{
  uint8_t valid;//0 or 1
  uint16_t kspeed;
  uint16_t cspeed;
}speed_t;

typedef struct{
  uint8_t flag;
  uint16_t addr;
  uint16_t dat;
}writeSpeed_t;
typedef struct{
  uint8_t idx;
  uint16_t addr;
  uint16_t sentivity;
  uint16_t angle;
  uint16_t gain;
  uint16_t power;
  uint16_t posneg;
  uint16_t strength;
  uint8_t verbuf[8];
}setSpeed_t;
enum{
  DEV_SPEED,
  DEV_LEVEL,
  DEV_BUS,
};
   
typedef struct{
  uint8_t recData;
  uint8_t timeout;
  uint16_t rxLen;
//  uint16_t txLen;
  uint16_t rxIdx;
//  uint8_t txBuf[HAL_UART_BUF_SIZE];
  uint8_t rxBuf[HAL_UART_BUF_SIZE];
}uart_t;

typedef struct{
  unsigned time100ms      : 1;
  unsigned time1s         : 1;
  unsigned time5s         : 1;
  unsigned modbus         : 1;
}uartflag_t;

typedef struct{
  uint8_t time100msCnt;
  uint8_t time500msCnt;
  uint8_t time1sCnt;
  uint8_t time5sCnt;
}uartperiod_t;

extern const modBusAttrRec_t ModBus_Attrs[];
extern const uint8_t C_MODBUS_ATTR_SIZE;
//Data
extern uint16_t gWaterLevel;//cm
extern uint16_t gWaterMinSpeed;
extern uint16_t gWaterSpeed;//mm/s
extern float gWaterFlow;//m3/s
extern uint16_t gHiWaterFlow;
extern uint16_t gLoWaterFlow;
extern uint32_t gWaterSumFlow;//m3/s
extern uint16_t gHiWaterSumFlow;
extern uint16_t gLoWaterSumFlow;
extern uint32_t gBleSumFlow;
extern uint16_t gMeterEmptyLevel;//cm
extern uint16_t gWaterFirstLevel;
extern uint16_t gMeterRange;//cm
extern uint16_t gMeterLowRange;//cm
extern uint16_t gFlowTolerance;//m3/s
extern uint8_t gMeterStatus;//
extern uint8_t gClearFlow;
extern uint16_t g10sRandSpeed;

extern uint16_t gSpeedSentivity;
extern uint16_t gSpeedAngle;
extern uint16_t gSpeedGain;
extern uint16_t gSpeedPower;

extern canel_t gCanelParam;

extern devInfo_t gDevInfo;

extern setSpeed_t gReadSpeed;

extern uint8_t gUart1RecByte;

extern uart_t gUart1RecBuf;

extern void HAL_UART_Initinal(void);

extern void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart);

extern void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart);

extern void HAL_UART3_RxCpltCallback(UART_HandleTypeDef *huart);

extern void HAL_UART4_RxCpltCallback(UART_HandleTypeDef *huart);

extern void HAL_UART5_RxCpltCallback(UART_HandleTypeDef *huart);

extern void HAL_UART_Preiod(void);

extern void HAL_UART_ProcessCmd(void);

extern uint8_t HAL_GSM_Send( uint8_t *msg, uint8_t len );

extern uint8_t HAL_BLE_Send( uint8_t *msg, uint8_t len );

extern uint8_t HAL_Bus_Send( uint8_t *msg, uint8_t len );

extern uint8_t HAL_Speed_Send( uint8_t *msg, uint8_t len );

extern uint8_t HAL_Level_Send( uint8_t *msg, uint8_t len );
#ifdef __cplusplus
}
#endif

#endif /*__STM32L0xx_MPR_H */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

